$.fn.extend({
    initCarousel: function (a) {
        this.each(function () {
            var i = {
                selectorPrevious: ".previous",
                selectorNext: ".next",
                selectorPaginationItems: ".pagination li",
                selectorContainer: ".container",
                selectorInnerContainer: ".inner-container",
                selectorItems: ".item",
                activeClass: "active",
                animationDuration: 400
            };
            $.extend(i, a);
            var f = {};
            f.carousel = $(this);
            f.prev = f.carousel.find(i.selectorPrevious);
            f.next = f.carousel.find(i.selectorNext);
            f.paginationItems = f.carousel.find(i.selectorPaginationItems);
            f.container = f.carousel.find(i.selectorContainer);
            f.innerContainer = f.container.find(i.selectorInnerContainer);
            f.items = f.innerContainer.find(i.selectorItems);
            f.activeItem = f.items.filter("." + i.activeClass);
            var j = {};
            j.itemsPerPage = Math.round(f.container.width() / f.items.width());
            j.totalPages = Math.ceil(f.items.length / j.itemsPerPage);
            j.pageWidth = 100;

            function e() {
                return f.carousel.data("activePage") || 0
            }
            function d(m, l) {
                l = (typeof (l) != "undefined") ? l : i.animationDuration;
                var c = 0;
                for (var k = ((m + 1) * j.itemsPerPage) - j.itemsPerPage; k < (m + 1) * j.itemsPerPage; ++k) {
                    var o = f.items.eq(k).outerHeight(true);
                    if (o > c) {
                        c = o
                    }
                }
                var n = {};
                n["margin-left"] = (0 - (j.pageWidth * m)) + "%";
                f.carousel.data("activePage", m);
                f.innerContainer.stop().animate(n, l, "swing", function () {
                    f.innerContainer.css("height", c);
                    f.carousel.trigger("pageChanged")
                });
                f.paginationItems.find("a").removeClass(i.activeClass);
                f.paginationItems.eq(m).find("a").addClass(i.activeClass)
            }
            function h() {
                var c = e() - 1;
                if (c >= 0) {
                    d(c)
                }
            }
            function g() {
                var c = e() + 1;
                if (c < j.totalPages) {
                    d(c)
                }
            }
            //f.innerContainer.swipeable();
            f.prev.click(function (c) {
                c.preventDefault();
                var k = f.carousel.filter(".swipeInProgress").add(f.carousel.find(".swipeInProgress")).length;
                if (!k) {
                    h()
                }
            });
            f.carousel.bind("swipeRight", h);
            f.next.click(function (c) {
                c.preventDefault();
                var k = f.carousel.filter(".swipeInProgress").add(f.carousel.find(".swipeInProgress")).length;
                if (!k) {
                    g()
                }
            });
            f.carousel.bind("swipeLeft", g);
            f.paginationItems.click(function (k) {
                k.preventDefault();
                var c = $(this).index();
                d(c)
            });
            f.carousel.bind("pageChanged", function () {
                var c = e();
                if (c == 0) {
                    f.prev.fadeOut(i.animationDuration)
                } else {
                    f.prev.fadeIn(i.animationDuration)
                }
                if (j.totalPages - (c + 1) == 0) {
                    f.next.fadeOut(i.animationDuration)
                } else {
                    f.next.fadeIn(i.animationDuration)
                }
            });
            f.carousel.bind("redrawCurrentPage", function (k, c) {
                d(e(), c)
            });
            $(window).bind("resize", function () {
                f.container.stabiliseCarousel(i.selectorItems);
                d(e())
            });
            //f.container.stabiliseCarousel(i.selectorItems);
            var b = 0;
            if (f.activeItem.length) {
                b = Math.floor((f.activeItem.index()) / j.itemsPerPage)
            }
            d(b, 0)
        });
        return this
    }
});

$(document).ready(function () {
    $(".carousel-container").initCarousel();
});